﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chbl.DAL;
using Chbl.Models;

namespace Chbl.Controllers.AdminRole
{
    [Authorize(Roles = "admin")]
    public class AdminGameController : Controller
    {
        private readonly ChblDb _db;

        public AdminGameController(ChblDb db)
        {
            _db = db;
        }

        //
        // GET: /AdminGame/

        public ViewResult Index()
        {
            var games = _db.Games.Include(g => g.HomeTeam).Include(g => g.RoadTeam).Include(g => g.SeasonDivision);
            return View(games.ToList());
        }

        //
        // GET: /AdminGame/Details/5

        public ViewResult Details(int id)
        {
            Game game = _db.Games.Find(id);
            return View(game);
        }

        //
        // GET: /AdminGame/Create

        public ActionResult Create()
        {
            ViewBag.HomeTeamId = new SelectList(_db.Teams, "Id", "Name");
            ViewBag.RoadTeamId = new SelectList(_db.Teams, "Id", "Name");
            ViewBag.SeasonDivisionId = new SelectList(_db.SeasonDivisions, "SeasonDivisionId", "SeasonDivisionId");
            return View();
        } 

        //
        // POST: /AdminGame/Create

        [HttpPost]
        public ActionResult Create(Game game)
        {
            if (ModelState.IsValid)
            {
                _db.Games.Add(game);
                _db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.HomeTeamId = new SelectList(_db.Teams, "Id", "Name", game.HomeTeamId);
            ViewBag.RoadTeamId = new SelectList(_db.Teams, "Id", "Name", game.RoadTeamId);
            ViewBag.SeasonDivisionId = new SelectList(_db.SeasonDivisions, "SeasonDivisionId", "SeasonDivisionId", game.SeasonDivisionId);
            return View(game);
        }
        
        //
        // GET: /AdminGame/Edit/5
 
        public ActionResult Edit(int id)
        {
            Game game = _db.Games.Find(id);
            ViewBag.HomeTeamId = new SelectList(GetTeamsQuery(game), "Id", "Name", game.HomeTeamId);
            ViewBag.RoadTeamId = new SelectList(GetTeamsQuery(game), "Id", "Name", game.RoadTeamId);
            ViewBag.SeasonDivisionId = new SelectList(_db.SeasonDivisions, "SeasonDivisionId", "Division.Name", game.SeasonDivisionId);
            return View(game);
        }

        private ICollection<Team> GetTeamsQuery(Game game)
        {
            return _db.SeasonDivisions.Find(game.SeasonDivisionId).Teams;
        }

        //
        // POST: /AdminGame/Edit/5

        [HttpPost]
        public ActionResult Edit(Game game)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(game).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.HomeTeamId = new SelectList(_db.Teams, "Id", "Name", game.HomeTeamId);
            ViewBag.RoadTeamId = new SelectList(_db.Teams, "Id", "Name", game.RoadTeamId);
            ViewBag.SeasonDivisionId = new SelectList(_db.SeasonDivisions, "SeasonDivisionId", "SeasonDivisionId", game.SeasonDivisionId);
            return View(game);
        }

        //
        // GET: /AdminGame/Delete/5
 
        public ActionResult Delete(int id)
        {
            Game game = _db.Games.Find(id);
            return View(game);
        }

        //
        // POST: /AdminGame/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Game game = _db.Games.Find(id);
            _db.Games.Remove(game);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}